package com.jimet.server;

import java.io.*;
import javax.servlet.http.*;

public class JimetSessionListener implements Serializable, HttpSessionListener, HttpSessionActivationListener, HttpSessionAttributeListener {


	private static final long serialVersionUID 		= 938160349075913654L;
	private static int sessionCount					= 0;
    private static int authenticatedSessionCount 	= 0;
    
	
    public synchronized void sessionCreated (HttpSessionEvent se) {
    	
        HttpSession newSession = se.getSession();
        newSession.setAttribute("listener", this);
        newSession.setMaxInactiveInterval(60*60); 
        sessionCount++;
    }

    public synchronized void sessionDestroyed (HttpSessionEvent se) {
        sessionCount--;
		HttpSession session = se.getSession();
		if(session.getAttribute("JIMET_LISTENER_ALERT") != null)
			authenticatedSessionCount--;
    }

    public synchronized void sessionDidActivate (HttpSessionEvent se) {
        sessionCount++;
		HttpSession session = se.getSession();
		
		if((session.getAttribute("JIMET_LISTENER_ALERT") != null))
			authenticatedSessionCount++;
    }

    public synchronized void sessionWillPassivate (HttpSessionEvent se) {
        sessionCount--;
		HttpSession session = se.getSession();
		
		if(session.getAttribute("JIMET_LISTENER_ALERT") != null)
			authenticatedSessionCount--;
    }
    
    
   public synchronized void attributeAdded(HttpSessionBindingEvent event) {
	  if(event.getName().equals("JIMET_LISTENER_ALERT")){
	        authenticatedSessionCount++;
	  }
   }

   public synchronized void attributeRemoved(HttpSessionBindingEvent event) {

	  if(event.getName().equals("JIMET_LISTENER_ALERT")){
	        System.out.println("azalttt?k");
  		authenticatedSessionCount--;
  	  }	  
   }

   public synchronized void attributeReplaced(HttpSessionBindingEvent event) {
   }
    
    
   public synchronized static int getSessionCount () {
	   return sessionCount;
   }

   public synchronized static int getAuthenticatedSessionCount () {
	   return authenticatedSessionCount;
   }

	
}
